package com.fan.play.media.capture

import com.fan.play.media.capture.video.Camera2Capture

/**
 * 视频画面的采集
 *
 * @param cameraID 相机ID
 * @param width 预览宽
 * @param height 预览高
 * @param nativeRef C++对象的指针
 */
class VideoCapture(cameraID: Int, width: Int, height: Int, nativeRef: Long) {

    //底层对象的引用
    private var mNativeRef: Long

    //具体的视频采集对象
    private var mCapture: ICapture

    private var mVideoCallback = object : IVideoCallback {

        override fun onVideoFrame(
            yBuf: ByteArray,
            uBuf: ByteArray,
            vBuf: ByteArray,
            rawType: Int,
            rotation: Int,
            isMirror: Boolean
        ) {
            onVideoFrame(yBuf, uBuf, vBuf, rawType, rotation, isMirror, nativeRef)
        }
    }

    init {
        mNativeRef = nativeRef
        mCapture = Camera2Capture(cameraID, width, height, mVideoCallback)
    }

    fun start(): Boolean {
        return mCapture.start()
    }

    fun stop() {
        mCapture.stop()
    }

    /**
     * 一帧画面传递到C++层，
     *
     * @param yBuf y分量
     * @param uBuf u分量
     * @param vBuf v分量
     * @param rawType 视频原始数据类型
     * @param rotation 旋转角度
     * @param isMirror 是否镜像
     * @param nativeRef C++对象的指针
     */
    private external fun onVideoFrame(
        yBuf: ByteArray,
        uBuf: ByteArray,
        vBuf: ByteArray,
        rawType: Int,
        rotation: Int,
        isMirror: Boolean,
        nativeRef: Long,
    )
}